-- SPDX-License-Identifier: GPL-3.0-or-later

local CardItem = require "packages.hearthstone.battlegrounds.core.card.carditem"


--- 炉石传说武器类
--- 
--- 武器装备在英雄上，可以主动攻击
--- 继承自CardItem，添加攻击力和耐久度
---
---@class Hearthstone.Weapon : Hearthstone.CardItem
---@field public atk integer @ 攻击力
---@field public durability integer @ 耐久度
---@field public maxDurability integer @ 最大耐久度
local Weapon = CardItem:subclass("Hearthstone.Weapon")

--- 构造函数
---@param name string @ 武器名称
---@param cost integer @ 法力值消耗
---@param atk integer @ 攻击力
---@param durability integer @ 耐久度
---@param rarity string @ 稀有度
---@param card_class string @ 职业
---@param card_set string @ 卡牌集
function Weapon:initialize(name, cost, atk, durability, rarity, card_class, card_set)
  CardItem.initialize(self, name, cost, CardItem.TypeWeapon, rarity, card_class, card_set)
  
  self.atk = atk or 0
  self.durability = durability or 1
  self.maxDurability = self.durability
end

function Weapon:__tostring()
  return "<Weapon " .. self.name .. " (" .. self.atk .. "/" .. self.durability .. ")>"
end

--- 克隆武器
---@param new_name string? @ 新名称
---@return Weapon
function Weapon:clone(new_name)
  local cloned = self.class:new(
    new_name or self.name,
    self.cost,
    self.atk,
    self.durability,
    self.rarity,
    self.card_class,
    self.card_set
  )
  
  -- 复制技能
  cloned.skills = {}
  for _, skill in ipairs(self.skills) do
    table.insert(cloned.skills, skill)
  end
  
  return cloned
end

--- 设置攻击力
---@param atk integer @ 攻击力
function Weapon:setAttack(atk)
  self.atk = atk
end

--- 设置耐久度
---@param durability integer @ 耐久度
function Weapon:setDurability(durability)
  self.durability = durability
  if durability > self.maxDurability then
    self.maxDurability = durability
  end
end

--- 增加攻击力
---@param amount integer @ 增加数量
function Weapon:addAttack(amount)
  self.atk = self.atk + amount
end

--- 增加耐久度
---@param amount integer @ 增加数量
function Weapon:addDurability(amount)
  self.durability = self.durability + amount
  self.maxDurability = self.maxDurability + amount
end

--- 武器攻击（消耗1点耐久度）
---@return boolean @ 是否攻击成功
function Weapon:attack()
  if self.durability <= 0 then
    return false
  end
  
  self.durability = self.durability - 1
  return true
end

--- 检查武器是否损坏
---@return boolean
function Weapon:isBroken()
  return self.durability <= 0
end

--- 检查武器是否满耐久
---@return boolean
function Weapon:isFullDurability()
  return self.durability >= self.maxDurability
end

--- 修复武器（恢复耐久度）
---@param amount integer @ 修复数量
---@return integer @ 实际修复量
function Weapon:repair(amount)
  local actual_repair = math.min(amount, self.maxDurability - self.durability)
  self.durability = self.durability + actual_repair
  return actual_repair
end

--- 获取武器描述（包含属性信息）
---@param lang string? @ 语言
---@return string
function Weapon:getDescription(lang)
  local desc = CardItem.getDescription(self, lang)
  local stats = self.atk .. "/" .. self.durability
  return desc .. " (" .. stats .. ")"
end

return Weapon
